//
//  XYFourthStepViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/9.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "XYFourthStepViewController.h"
#import "XYFifthStepViewController.h"
@interface XYFourthStepViewController ()

@end

@implementation XYFourthStepViewController
static const NSString * KEY_STEP  = @"step4";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"setp4 record === %@", self.NewRecordArray.firstObject.getDetailDict);
    [self setNaviBarTitle:self.NewRecordArray.firstObject.name];
    _maintableview=[[UITableView alloc]initWithFrame:CGRectMake(10, 64, fWidth_Current-20, fHeight_Current-64-50)];
    _maintableview.backgroundColor = [UIColor clearColor];
    _maintableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _maintableview.dataSource = self;
    _maintableview.delegate = self;
    _maintableview.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_maintableview];
    
    UIImageView*buttomview=[[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight_Current-50, fWidth_Current, 50)];
    buttomview.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
    [self.view addSubview:buttomview];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 50)];
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=1;
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=[UIColor lightGrayColor];
    label.text=@"当下是第四步4/6";
    [buttomview addSubview:label];
    onefinished=@"0";
    twofinished=@"0";

    if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
        [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > 0 &&
        [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER]) {
        
        onefinished = [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER];
    }
    
    if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
        [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > 0 &&
        [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:1] objectForKey:KEY_RECORD_ANSWER]) {
        
        twofinished = [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:1] objectForKey:KEY_RECORD_ANSWER];
    }
}

#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==2) {
        return 1;
        
    }else{
        return 2;
        
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;//返回标题数组中元素的个数来确定分区的个数
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        if (indexPath.row==1) {
            return 50;
        }else{
            NSString*titlestr=@"4.1、EDTA-K2抗凝管（紫盖）轻柔搓管10次。";
            
            CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
            
            return 40+hight.height;
        }
        
    }else if (indexPath.section == 1){
        
        if (indexPath.row == 1) {
            
            return 50;
        }else{
            NSString*titlestr=@"4.2、血清管（黄盖）轻柔颠倒5次。";
            CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
            
            return 40+hight.height;
        }
    }else{
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*headview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 18)];
    headview.backgroundColor=[UIColor clearColor];
    return headview;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strIdentifier = @"strIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        
    }
    switch (indexPath.section) {
        case 0:
            if (indexPath.row==0) {
                NSString*titlestr=@"4.1、EDTA-K2抗凝管（紫盖）轻柔搓管10次。";
                
                CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
                UILabel *titlelab=[[UILabel alloc]initWithFrame:CGRectMake(10, 20, fWidth_Current-40, hight.height)];
                titlelab.textColor=[UIColor whiteColor];
                titlelab.backgroundColor=[UIColor clearColor];
                titlelab.text=titlestr;
                titlelab.numberOfLines=0;
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, hight.height+40, fWidth_Current-20, 0.5)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:titlelab];
                [cell addSubview:lineview];
            }else{
                UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
                btn1.frame=CGRectMake(50, 15, 70, 20);
                [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0 ,0.0, 50)];
                [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-30.0 ,0.0, 0)];
                
                [btn1 setTitle:@"  完成" forState:UIControlStateNormal];
                [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn1 setTitleColor:[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0] forState:UIControlStateSelected];
                btn1.titleLabel.font=[UIFont systemFontOfSize:15];
                [btn1 setImage:[UIImage imageNamed:@"selectbtn"] forState:UIControlStateNormal];
                [btn1 setImage:[UIImage imageNamed:@"selectbtn_f"] forState:UIControlStateSelected];
                
                [cell addSubview:btn1];
                UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
                btn2.frame=CGRectMake(50+110, 15, 80, 20);
                [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0 ,0.0, 60)];
                [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-30.0 ,0.0, 0)];
                [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn2 setTitleColor:[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0] forState:UIControlStateSelected];
                [btn2 setTitle:@"  未完成" forState:UIControlStateNormal];
                btn2.titleLabel.font=[UIFont systemFontOfSize:15];
                [btn2 setImage:[UIImage imageNamed:@"selectbtn"] forState:UIControlStateNormal];
                [btn2 setImage:[UIImage imageNamed:@"selectbtn_f"] forState:UIControlStateSelected];
                
                [cell addSubview:btn2];
                btnno1=btn1;
                btnyes1=btn2;
                if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
                    [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > indexPath.section &&
                    [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:indexPath.section] objectForKey:KEY_RECORD_ANSWER]) {
                    
                    onefinished = [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:indexPath.section] objectForKey:KEY_RECORD_ANSWER];
                }
                if ([onefinished isEqualToString:@"1"]) {
                    btnyes1.selected=YES;
                    btnno1.selected=NO;
                    
                }else {
                    btnno1.selected=YES;
                    btnyes1.selected=NO;
                }
                
                if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                    [btnyes1 removeTarget:self action:@selector(yesclick:) forControlEvents:UIControlEventTouchUpInside];
                    [btnno1 removeTarget:self action:@selector(noclick:) forControlEvents:UIControlEventTouchUpInside];
                }else{
                    [btnyes1 addTarget:self action:@selector(yesclick:) forControlEvents:UIControlEventTouchUpInside];
                    [btnno1 addTarget:self action:@selector(noclick:) forControlEvents:UIControlEventTouchUpInside];
                }
            }
            break;
        case 1:
            if (indexPath.row==0) {
                NSString*titlestr=@"4.2、血清管（黄盖）轻柔颠倒5次。";
                
                CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
                UILabel *titlelab=[[UILabel alloc]initWithFrame:CGRectMake(10, 20, fWidth_Current-40, hight.height)];
                titlelab.textColor=[UIColor whiteColor];
                titlelab.backgroundColor=[UIColor clearColor];
                titlelab.text=titlestr;
                titlelab.numberOfLines=0;
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, hight.height+40, fWidth_Current-20, 0.5)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:titlelab];
                [cell addSubview:lineview];
            }else{
                UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
                btn1.frame=CGRectMake(50, 15, 70, 20);
                [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0 ,0.0, 50)];
                [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-30.0 ,0.0, 0)];
                
                [btn1 setTitle:@"  完成" forState:UIControlStateNormal];
                [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn1 setTitleColor:[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0] forState:UIControlStateSelected];
                btn1.titleLabel.font=[UIFont systemFontOfSize:15];
                [btn1 setImage:[UIImage imageNamed:@"selectbtn"] forState:UIControlStateNormal];
                [btn1 setImage:[UIImage imageNamed:@"selectbtn_f"] forState:UIControlStateSelected];
                
                [cell addSubview:btn1];
                UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
                btn2.frame=CGRectMake(50+110, 15, 80, 20);
                [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0 ,0.0, 60)];
                [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-30.0 ,0.0, 0)];
                [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn2 setTitleColor:[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0] forState:UIControlStateSelected];
                [btn2 setTitle:@"  未完成" forState:UIControlStateNormal];
                btn2.titleLabel.font=[UIFont systemFontOfSize:15];
                [btn2 setImage:[UIImage imageNamed:@"selectbtn"] forState:UIControlStateNormal];
                [btn2 setImage:[UIImage imageNamed:@"selectbtn_f"] forState:UIControlStateSelected];
                
                [cell addSubview:btn2];
                btnno2=btn1;
                btnyes2=btn2;
                if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
                    [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > indexPath.section &&
                    [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:indexPath.section] objectForKey:KEY_RECORD_ANSWER]) {
                    
                    twofinished = [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:indexPath.section] objectForKey:KEY_RECORD_ANSWER];
                }
                if ([twofinished isEqualToString:@"1"]) {
                    btnyes2.selected=YES;
                    btnno2.selected=NO;
                    
                }else {
                    btnno2.selected=YES;
                    btnyes2.selected=NO;
                }
                
                if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                    [btnyes2 removeTarget:self action:@selector(yesclick:) forControlEvents:UIControlEventTouchUpInside];
                    [btnno2 removeTarget:self action:@selector(noclick:) forControlEvents:UIControlEventTouchUpInside];
                }else{
                    [btnyes2 addTarget:self action:@selector(yesclick:) forControlEvents:UIControlEventTouchUpInside];
                    [btnno2 addTarget:self action:@selector(noclick:) forControlEvents:UIControlEventTouchUpInside];
                }
            }
            break;
        case 2:{
            cell.backgroundColor=[UIColor clearColor];
            
            if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                
                UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
                btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn2.layer.cornerRadius=5;
                [btn2 setTitle:@"下一步" forState:UIControlStateNormal];
                btn2.clipsToBounds=YES;
                [btn2 addTarget:self action:@selector(nextStepClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn2];
            }else{
                UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-30)/2, 50)];
                btn1.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
                btn1.layer.cornerRadius=5;
                [btn1 setTitle:@"取消" forState:UIControlStateNormal];
                btn1.clipsToBounds=YES;
                [btn1 addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn1];
                UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake((fWidth_Current-30)/2+10, 0, (fWidth_Current-30)/2, 50)];
                btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn2.layer.cornerRadius=5;
                [btn2 setTitle:@"保存" forState:UIControlStateNormal];
                btn2.clipsToBounds=YES;
                [btn2 addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn2];
            }
        }
            break;
            
        default:
            break;
    }
    return cell;
}
-(void)yesclick:(UIButton *)sender{
    sender.selected=YES;
    if (sender == btnyes1) {
        btnno1.selected=NO;
        onefinished=@"1";
    }else if (sender == btnyes2) {
        btnno2.selected=NO;
        twofinished=@"1";
    }
}
-(void)noclick:(UIButton *)sender{
    sender.selected=YES;
    
    if (sender == btnno1) {
        btnyes1.selected=NO;
        onefinished=@"0";
        
    }else if (sender == btnno2) {
        btnyes2.selected=NO;
        twofinished=@"0";
    }
}
-(void)saveClick{
    [self creatDetailDict];
   
    XYFifthStepViewController *XYFifthStepVC = [[XYFifthStepViewController alloc] init];
    XYFifthStepVC.NewRecordArray = self.NewRecordArray;
    [self.navigationController pushViewController:XYFifthStepVC animated:YES];
}
-(void)creatDetailDict{
    
    NSDictionary *question1Dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"4.1、EDTA-K2抗凝管（紫盖）轻柔搓管10次。", KEY_RECORD_QUESTION,
                                  TYPE_QUESTION_SELECT, KEY_RECORD_TYPE,
                                  onefinished, KEY_RECORD_ANSWER, nil];
   
    NSDictionary *question2Dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"4.2、血清管（黄盖）轻柔颠倒5次。", KEY_RECORD_QUESTION,
                                  TYPE_QUESTION_SELECT, KEY_RECORD_TYPE,
                                  twofinished, KEY_RECORD_ANSWER, nil];
    
    
    NSArray *qustionArr = [NSArray arrayWithObjects:question1Dict, question2Dict, nil];
    for (Record *oneRecord in self.NewRecordArray) {
        NSMutableDictionary *tempDict = [oneRecord getDetailDict];
        [tempDict setObject:qustionArr forKey:KEY_STEP];
        [oneRecord setDetailDict:tempDict];
        NSLog(@"tempDict %@", tempDict);
        oneRecord.recordDetail = [tempDict JSONString];
        oneRecord.savedStepCount = 4;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [oneRecord update];
        });
    }
   
}
-(void)cancelclick{
    UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
    [self.navigationController popToViewController:miloViewControl animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)nextStepClick{
    XYFifthStepViewController *XYFifthStepVC = [[XYFifthStepViewController alloc] init];
    XYFifthStepVC.NewRecordArray = self.NewRecordArray;
    XYFifthStepVC.fromtype = @"search";
    [self.navigationController pushViewController:XYFifthStepVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
